package logic

import (
	"context"
	"fmt"
	"gorm.io/gorm"
	"qimao-fiction/models/mysql"

	"qimao-fiction/qimaorpc/internal/svc"
	"qimao-fiction/qimaorpc/qimaorpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type NovelUpdateLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewNovelUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *NovelUpdateLogic {
	return &NovelUpdateLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *NovelUpdateLogic) NovelUpdate(in *qimaorpc.NovelUpdateRequest) (*qimaorpc.NovelUpdateResponse, error) {
	// todo: add your logic here and delete this line

	var novel mysql.Novel
	mysql.DB.Debug().Where("id=?", in.Id).Find(&novel)

	if novel.ID == 0 {
		return nil, fmt.Errorf("小说不存在")
	}
	m := mysql.Novel{
		Model:   gorm.Model{ID: uint(in.Id)},
		Title:   in.Title,
		Content: in.Content,
		Image:   in.Image,
	}
	mysql.DB.Debug().Updates(&m)
	return &qimaorpc.NovelUpdateResponse{
		NovelId: int64(m.ID),
	}, nil
}
